<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :form_data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = item.type == 'input_number' ? ['', ''] : ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div>提成汇总</div>
        <div class="flex">
          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        border
        v-loading="loading"
        :row-style="{
          height:
            height_size == 1 ? '58px' : height_size == 2 ? '48px' : '38px',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="项目名称"
          prop="name"
          key="项目名称"
          sortable
          v-if="header_list[0].showTable"
        ></el-table-column>
        <el-table-column
          label="开始时间"
          prop="actualStartTime"
          key="开始时间"
          sortable
          v-if="header_list[1].showTable"
        >
          <template #default="scope">
            <div v-if="scope.row.actualStartTime ?? '' !== ''">
              {{
                formatDate(parseInt(scope.row.actualStartTime), 'YYYY-MM-DD')
              }}
            </div>
            <div v-else>-- --</div>
          </template>
        </el-table-column>

        <el-table-column
          label="设计经理"
          prop="managerName"
          key="设计经理"
          sortable
          v-if="header_list[2].showTable"
        ></el-table-column>
        <el-table-column
          label="设计组长"
          prop="groupLeaderName"
          key="设计组长"
          sortable
          v-if="header_list[3].showTable"
        ></el-table-column>
        <el-table-column
          label="设计部门"
          prop="deptName"
          key="设计部门"
          sortable
          v-if="header_list[4].showTable"
        ></el-table-column>
        <el-table-column
          label="总提成金额"
          prop="projectTotalCommission"
          key="总提成金额"
          sortable
          v-if="header_list[5].showTable"
        >
        </el-table-column>
        <el-table-column
          label="首次提成金额"
          prop="firstCommissionAmount"
          key="首次提成金额"
          sortable
          v-if="header_list[6].showTable"
        ></el-table-column>

        <el-table-column
          label="二次提成金额"
          prop="secondCommissionAmount"
          key="二次提成金额"
          sortable
          v-if="header_list[7].showTable"
        >
        </el-table-column>
        <el-table-column
          label="首次提成发放月份"
          prop="firstCommissionMonth"
          key="首次提成发放月份"
          sortable
          v-if="header_list[8].showTable"
        >
        </el-table-column>
        <el-table-column
          label="二次提成发放月份"
          prop="secondCommissionMonth"
          key="二次提成发放月份"
          sortable
          v-if="header_list[9].showTable"
        >
        </el-table-column>
        <el-table-column
          label="集体扣款分担金额"
          prop="teamDeductionAmount"
          key="集体扣款分担金额"
          sortable
          v-if="header_list[10].showTable"
        >
        </el-table-column>
        <el-table-column
          label="个人扣款金额"
          prop="personalDeductionAmount"
          key="个人扣款金额"
          sortable
          v-if="header_list[11].showTable"
        >
        </el-table-column>
        <el-table-column
          label="集体奖励分担金额"
          key="集体奖励分担金额"
          sortable
          prop="teamRewardAmount"
          v-if="header_list[12].showTable"
        ></el-table-column>
        <el-table-column
          label="个人奖励金额"
          key="个人奖励金额"
          sortable
          prop="personalRewardAmount"
          v-if="header_list[13].showTable"
        ></el-table-column>
        <el-table-column
          label="设计经理提成"
          key="设计经理提成"
          sortable
          prop="managerCommission"
          v-if="header_list[14].showTable"
        ></el-table-column>
        <el-table-column
          label="设计组长提成"
          key="设计组长提成"
          sortable
          prop="groupLeaderCommission"
          v-if="header_list[15].showTable"
        ></el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="list_debounce()"
        @current-change="list_debounce()"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import {
  check_role,
  formatDate,
  debounce,
  handle_search_key,
} from '@/utils/check_role' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted } from 'vue'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { ElMessage } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const list = ref()
const total = ref()
const max_height = ref()
const loading = ref(false)
const height_size = ref(1)
const header_list = ref<any>([
  {
    label: '项目名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '开始时间',
    value: '',
    showTable: true,
    type: 'date',
    isShow: true,
    options: [],
  },
  {
    label: '设计经理',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '设计组长',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '设计部门',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
  {
    label: '总提成金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '首次提成金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '二次提成金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '首次提成发放月份',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '二次提成发放月份',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '集体扣款分担金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '个人扣款金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '集体奖励分担金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '个人奖励金额',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '设计经理提成',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
  {
    label: '设计组长提成',
    value: ['', ''],
    showTable: true,
    type: 'input_number',
    isShow: false,
    options: [],
  },
])
const page_data = ref({
  current: 1,
  pageSize: 10,
  name: '',
  actualStartTime: '',
  managerName: '',
  groupLeaderName: '',
  deptName: '',
  projectTotalCommission: '',
  firstCommissionAmount: '',
  secondCommissionAmount: '',
  firstCommissionMonth: '',
  secondCommissionMonth: '',
  teamDeductionAmount: '',
  personalDeductionAmount: '',
  teamRewardAmount: '',
  personalRewardAmount: '',
  managerCommission: '',
  groupLeaderCommission: '',
})

onMounted(() => {
  max_height.value = window.innerHeight - 320 // 带分页的 table 高度
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('刷新成功！')
  })
  get_list()
  //   get_myProjectTaskList();
})
onUnmounted(() => {
  EventBus.off('refresh')
})

const get_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.taskCommissionTotalPage,
      method: 'post',
      data: handle_search_key(page_data.value),
    })
    .then((res) => {
      console.log(res)
      list.value = res.data.content
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((err) => {
      ElMessage.error(err)
      loading.value = false
    })
}
let list_debounce = debounce(200, get_list)
const search_key = () => {
  page_data.value = {
    current: 1,
    pageSize: 10,
    name: header_list.value[0].value,
    actualStartTime: header_list.value[1].value,
    managerName: header_list.value[2].value,
    groupLeaderName: header_list.value[3].value,
    deptName: header_list.value[4].value,
    projectTotalCommission: header_list.value[5].value,
    firstCommissionAmount: header_list.value[6].value,
    secondCommissionAmount: header_list.value[7].value,
    firstCommissionMonth: header_list.value[8].value,
    secondCommissionMonth: header_list.value[9].value,
    teamDeductionAmount: header_list.value[10].value,
    personalDeductionAmount: header_list.value[11].value,
    teamRewardAmount: header_list.value[12].value,
    personalRewardAmount: header_list.value[13].value,
    managerCommission: header_list.value[14].value,
    groupLeaderCommission: header_list.value[15].value,
  }
  get_list()
}
</script>

<style lang="scss" scoped>
.top_item {
  border: 1px solid #ccc;
  cursor: pointer;
  padding: 5px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  &:hover {
    color: #3580eb;
    border-color: #3580eb;
  }
}
.line {
  background-color: #333;
  width: 1px;
  height: 16px;
  margin: 0 10px;
}
</style>
